package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.baihang.BaihangAirCreditData;
import com.ucredit.babel.common.model.third.baihang.BaihangAirCreditRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 百行航旅
 *
 * @author szw
 */

public class BaihangAirCreditParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 百行航旅
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BaihangAirCreditRecord obj = (BaihangAirCreditRecord) model;
        BaihangAirCreditData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder bhSb = new StringBuilder(2000);
        bhSb.append(data.getHead().getRequestRefId() == null ? "" : data.getHead().getRequestRefId()).append(CRT001);
        bhSb.append(data.getHead().getResponseRefId() == null ? "" : data.getHead().getResponseRefId()).append(CRT001);
        bhSb.append(data.getHead().getResult() == null ? "" : data.getHead().getResult()).append(CRT001);
        bhSb.append(data.getHead().getResponseCode() == null ? "" : data.getHead().getResponseCode()).append(CRT001);
        bhSb.append(data.getHead().getResponseMsg() == null ? "" : data.getHead().getResponseMsg()).append(CRT001);
        bhSb.append(data.getResponse().getFlightScore() == null ? "" : data.getResponse().getFlightScore()).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + bhSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_baihang_aircredit_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIHANG_AIR_CREDIT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百行航旅";
    }
}
